layui.extend({
    baseConfig : "/js/pages/baseConfig"
}).use(['form','layer','table','element','util','baseConfig'],function(){

    var ids = getUrlParamer( "ids");//标识是否是查看 商品发货的的商品列表
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    //获取浏览器最大高度，以防页面显示不完全
    var pageMaxHeight = $(document).height();

    var tableIns = table.render({
        elem: '#list',
        url : '/goods/listNoPage?status=1&storeCount=1&ids=' + ( ids ? ids : ""),
        cellMinWidth : 95,
        height : "full",
        text: { none: "Aucun résultat"},
        id : "listTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50, event:"chooseOrNot", hide: ids && ids != "" ? true : false},
            {field: 'name', title: "Articles", width:250, align:"center", templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.name + "</div>";
            }},
            {field: 'number', title: "Référence", width:200, align:"center"},
            {field: 'customHouse', title: "HS CODE", align:"center"},
            {field: 'storeCount', title: "Quantité", align:"center",templet : function( d) {
                return "1";
            }},
            {field: 'price', hide: ids != "" ? true : false, title: "销售价", align:"center",templet : function( d) {
                return parseFloat( Math.abs( d.sellPrice)).toFixed(2)
            }},
            //选择发货商品和查看出关单填写信息展示的金额列不同
            {field: 'sellPrice', hide: ids != "" ? false : true, title: 'HT VENTE', align:'center', templet : function( d){
                return parseFloat( Math.abs( d.sellPrice)).toFixed(2);
            }},
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data //解析数据列表
            };
        }
    });

    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var checkStatus = table.checkStatus( 'listTable'),
            data = checkStatus.data;
        var moneyCount = parseFloat( $("input[name=totalCount]").val());
        if( moneyCount == 0){
            layer.msg( "请选择需要发货的商品");
            return;
        }
        /*
        if( moneyCount > 8000){
            layer.msg( "单次发货金额不能大于8000");
            return;
        }*/
        var ids = "";
        var checkStatus = table.checkStatus( 'listTable'),
            data = checkStatus.data, ids = "";
        if( data.length > 0) {
            for ( var i=0 ; i<data.length ; i++) {
                ids += data[i].id;
                if( i != data.length-1){
                    ids += ",";
                }
            }
        }

        //提交数据
        $.post("/sendGoodsRecord/sendGoods",{
            totalAmount : moneyCount,
            goodsIds : ids
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "商品发货成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });


    $("#deleteDiv").on("click",function(){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        var ids = "";
        var checkStatus = table.checkStatus( 'listTable'),
            data = checkStatus.data, ids = "";
        if( data.length > 0) {
            for ( var i=0 ; i<data.length ; i++) {
                ids += data[i].id;
                if( i != data.length-1){
                    ids += ",";
                }
            }
        }
        if( !ids || ids == ""){
            layer.msg( "请选择需要删除的商品");
            return;
        }

        //提交数据
        $.post("/goods/del?ids=" + ids,{
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "商品删除成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //查看的话隐藏提交按钮
    if( ids && ids != ""){

        $("#submitDiv").hide();
    }

    table.on('checkbox(list)', function(obj){
        var checkStatus = table.checkStatus( 'listTable'),
            data = checkStatus.data;
        var moneyCount = 0.00;
        if( data.length > 0) {
            for ( var i=0 ; i<data.length ; i++) {
                moneyCount += Math.abs( parseFloat( data[i].price));
            }
        }
        $("input[name=totalCount]").val( moneyCount.toFixed( 2));
    });
});
